package com.wp.targetOffer;

/**
 * Created by 王萍 on 2017/8/12 0012.
 */

/**
 * 操作给定的二叉树，将其变换为源二叉树的镜像。
 */
public class 二叉树的镜像 {

    public static void main(String[] args) {

        TreeNode node8 = new TreeNode(8);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        TreeNode node9 = new TreeNode(9);
        TreeNode node10 = new TreeNode(10);
        TreeNode node11 = new TreeNode(11);

        node8.left = node6;
        node8.right = node10;
        node6.left = node5;
        node6.right = node7;
        node10.left = node9;
        node10.right = node11;

        printTree(node8);
        System.out.println();
        Mirror(node8);
        printTree(node8);
    }

    private static void printTree(TreeNode root) {
        if (root==null)return;
        System.out.print(root.val+"  ");
        printTree(root.left);
        printTree(root.right);
    }

    public static void Mirror(TreeNode root) {
        if (root==null)return;//null判断
        TreeNode swapNode;//用于两个节点交换的临时节点
        swapNode=root.left;
        root.left=root.right;
        root.right=swapNode;
        Mirror(root.left);
        Mirror(root.right);
    }

    private static class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;
        }
    }
}
